//
//  PageNavigatorController.swift
//  PageNavigator
//
//  Created by 张奇 on 2025/8/13.
//

import UIKit

public class PageNavigatorController: UIViewController {
    
    private weak var segment: SegmentViewController!
    private weak var container: PageContainerViewController!
    
    private let segmentDataSource: SegmentItemDataSource
    private let containerDataSource: PageContainerDataSource
    private let indicatorDataSource: SegmentIndicatorDataSource
    
    public init(segmentDataSource: SegmentItemDataSource,
                containerDataSource: PageContainerDataSource,
         indicatorDataSource: SegmentIndicatorDataSource) {
        self.segmentDataSource = segmentDataSource
        self.containerDataSource = containerDataSource
        self.indicatorDataSource = indicatorDataSource
        super.init(nibName: nil, bundle: nil)
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    public override func viewDidLoad() {
        super.viewDidLoad()

        // Do any additional setup after loading the view.
        
        initUI()
    }
    
    func initUI() {
        let segment = SegmentViewController()
        segment.delegate = self
        segment.dataSource = segmentDataSource
        self.attachChildViewController(segment)
        self.segment = segment
        
        indicatorDataSource.attach { indicators in
            indicators.forEach { indicator in
                segment.addIndicator(indicator)
            }
        }
        
        let container = PageContainerViewController()
        container.delegate = self
        container.dataSource = containerDataSource
        self.attachChildViewController(container)
        self.container = container
        
        self.segment.select(index: 0)
    }
    
    override public func viewDidLayoutSubviews() {
        super.viewDidLayoutSubviews()
        let w = self.view.bounds.width
        let segmentHeight = segmentDataSource.getSegmentItemHeight()
        let containerHeight = self.view.bounds.height - segmentHeight;
        self.segment.view.frame = CGRect(x: 0, y: 0, width: w, height: segmentHeight)
        self.container.view.frame = CGRect(x: 0, y: segmentHeight, width: w, height: containerHeight)
    }
    
    public override func didMove(toParentViewController parent: UIViewController?) {
        super.didMove(toParentViewController: parent)
        if (nil != parent) {
            segment.didMove(toParentViewController: self)
            container.didMove(toParentViewController: self)
        } else {
            segment.didMove(toParentViewController: nil)
            container.didMove(toParentViewController: nil)
        }
    }

    public override func willMove(toParentViewController parent: UIViewController?) {
        super.willMove(toParentViewController: parent)
    }


}

extension PageNavigatorController: PageContainerViewControllerDelegate {
    
    public func pageContainerViewController(_ content: PageContainerViewController, scrollingFrom from: Int, to: Int, percent: CGFloat) {
        self.segment.scrolling(from: from, to: to, percent: percent)
    }
    
    /// 完全滚动到指定位置 可选
    /// - Parameters:
    ///   - content: 容器
    ///   - didScrollTo: 滚动到位置
    
    public func pageContainerViewController(_ content: PageContainerViewController, didScrollTo to: Int) {
        self.segment.select(index: to)
    }
}

extension PageNavigatorController: SegmentViewControllerDelegate {
    
    public func pageSegmentViewController(_ content: SegmentViewController, didSelected selected: Int) {
        self.container.select(index: selected)
    }

}
